var util = require('../../libs/util.js');
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    pageSize: 10,
    pageNumber: 0,
    count: 0,
    page: 1,
    firstPage: true,
    prevPage: true,
    nextPage: true,
    lastPage: true,
    role: 'finance',
    userType:'pre'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadData();
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  search: function () {
    this.loadData();
  },
  loadData: function () {
    var that = this;
    util.getData("/atapi/user/user/userOfRole", {
      'CO_ID': app.globalData.CO_ID,
      'role': that.data.role,
      'searchVal': that.data.searchVal,
      'page': that.data.page,
      'pageSize': that.data.pageSize,
      'userType': that.data.userType
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        var count = res.data.data[1][0].COUNT;
        var pageNumber = Math.ceil(count / that.data.pageSize);
        that.setData({
          dataList: res.data.data[0],
          count: count,
          pageNumber: pageNumber,
          page: that.data.page == 0 ? 1 : that.data.page
        });
        that.setPageStatus();
      } else {
        that.setData({
          page: 0,
          dataList: []
        });
      }
    });
  },
  searchUser: function (e) {
    this.loadOtherUserData()
  },
  changeTab: function (e) {
    var that = this;
    var usertype = e.currentTarget.dataset.usertype;
    that.setData({
      userType: usertype
    })
    if(usertype != 'pre'){
      that.loadOtherUserData();
    }
  },
  loadOtherUserData: function () {
    var that = this;
    util.getData("/atapi/user/user/userOfOther", {
      'searchVal': that.data.searchUserVal,
      'CO_ID': app.globalData.CO_ID,
      'role': that.data.role,
      'searchUserPhone': that.data.searchUserPhone,
      'searchUserName': that.data.searchUserName,
      'userType': that.data.userType
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        that.setData({
          userList: res.data.data
        });
      } else {
        that.setData({
          userList: []
        });
      }
    });
  },
  addRole: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    //var roles = e.currentTarget.dataset.roles;
    // if (roles == '' || roles == null) {
    //   roles = that.data.role;
    // } else {
    //   roles = roles.replace("null,", '').replace(",null", '').replace("null", '');
    //   roles += ',' + that.data.role;
    // }
    util.putData("/atapi/user/user/setRole", {
      'CO_ID': app.globalData.CO_ID,
      'user_id': id,
      'add': that.data.role
    }, function (res, error) {
      console.log(res); console.log(error)
      if (res.data.success) {
        wx.showToast({
          title: "设置角色成功",
          icon: "success",
          duration: 3000,
          success: function () {
            setTimeout(function () {
              that.hideModal();
              that.loadData();
            }, 3000);
          }
        });
      } else {
        wx.showToast({
          title: "设置角色失败",
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  deleteRole: function (e) {
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '确定删除吗？',
      success(res) {
        if (res.confirm) {
          var id = e.currentTarget.dataset.id;
          //var roles = e.currentTarget.dataset.roles;
          //roles = roles.replace("," + that.data.role, "").replace(that.data.role + ",", "").replace(that.data.role, "");
          util.putData("/atapi/user/user/setRole", {
            'CO_ID': app.globalData.CO_ID,
            'user_id': id,
            'delete': that.data.role
          }, function (res, error) {
            console.log(res); console.log(error)
            if (res.data.success) {
              wx.showToast({
                title: "删除角色成功",
                icon: "success",
                duration: 3000,
                success: function () {
                  setTimeout(function () {
                    that.hideModal();
                    that.loadData();
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: "删除角色失败",
                image: '/images/wrong.png',
                duration: 3000
              });
            }
          });
        }

      }
    })
    
  },
  prerecordedSubmit: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    if(!that.data.prerecordedUser){
      wx.showToast({
        title: "请输入预录姓名",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false
    }
    if(!that.data.prerecordedPhone){
      wx.showToast({
        title: "请输入预录手机",
        image: '/images/wrong.png',
        duration: 3000
      });
      return false
    }
    wx.showLoading({
      title: '提交中...',
    })
    util.postData("/atapi/user/user/prerecorded", {
      'CO_ID': app.globalData.CO_ID,
      'CO_NAME': app.globalData.CO_NAME,
      'name': that.data.prerecordedUser,
      'phone': that.data.prerecordedPhone,
      'role':that.data.role
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading()
      if (res.data.success) {
        wx.showModal({
          title: '温馨提示',
          content: '新增预录用户成功',
          cancelText:'继续新增',
          confirmText:'关闭',
          success(res) {
            if (res.confirm) {
              that.hideModal();
              that.loadData();
            }
            that.setData({
              prerecordedUser:'',
              prerecordedPhone:''
            })
          }
        });
      } else {
        wx.showModal({
          title: '错误提示',
          content: res.data.data,
          success(res) {
            if (res.confirm) {
            }
          }
        });
      }
    });
  },
  //翻页按钮状态
  setPageStatus: function () {
    var that = this;
    var page = that.data.page, pageNumber = that.data.pageNumber;
    if (pageNumber == 1) {
      that.setData({
        firstPage: true,
        prevPage: true,
        nextPage: true,
        lastPage: true
      });
    } else if (pageNumber > 1) {
      if (page == 1) {
        that.setData({
          firstPage: true,
          prevPage: true,
          nextPage: false,
          lastPage: false
        });
      } else if (page == pageNumber) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: true,
          lastPage: true
        });
      } else if (page < pageNumber && page > 1) {
        that.setData({
          firstPage: false,
          prevPage: false,
          nextPage: false,
          lastPage: false
        });
      }
    }
  },
  //分页按钮操作
  pageChange: function (e) {
    var type = e.currentTarget.dataset.page;
    if (type == 'first') {
      this.setData({
        page: 1
      })
      this.loadData();
    }
    if (type == 'last') {
      this.setData({
        page: this.data.pageNumber
      })
      this.loadData();
    }
    if (type == 'prev') {
      this.setData({
        page: this.data.page - 1
      })
      this.loadData();
    }
    if (type == 'next') {
      this.setData({
        page: this.data.page + 1
      })
      this.loadData();
    }
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
    this.loadOtherUserData();
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})